/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package caintra.Sapra.Pantalla.Ajuste;

import caintra.Sapra.Internacional.DBManager;
import caintra.Sapra.Model.Facturaajuste;
import caintra.Sapra.Model.Practicante;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Administrador
 */
public class AjusteQuery {

    public List<Facturaajuste> getAjustes(int fk_periodo,ArrayList<Practicante> Lista_Detalle,int opc){
        List<Facturaajuste> lista;
        Facturaajuste aj;
        String sql="";
        Practicante p;
        try {
            sql="SELECT f.*,u.login AS login FROM facturaajuste f"
                    + " INNER JOIN usuario u"
                    + " ON f.fk_usuarioId = u.usuarioId"
                    + " WHERE f.fk_periodoId="+fk_periodo+" AND (";

            for(int i=0;i<Lista_Detalle.size();i++){
                 p=(Practicante)Lista_Detalle.get(i);
                 sql+="f.fk_convenioid="+p.getPracticanteId();
                 if((i+1)<Lista_Detalle.size())
                     sql += " OR ";
            }
            sql+=")"
                    + " AND interno="+opc
                    + " ORDER BY f.tiempo DESC";


            lista=DBManager.getQuery(Facturaajuste.class,sql);
            
        } catch (Exception e) {
            return null;
        }
        return lista;

    }


    public boolean insertAjuste(Facturaajuste ajuste){
        String sql="";
        try {
            sql="INSERT INTO facturaajuste(factor,cometario,tiempo,fk_periodoid,fk_convenioid,interno,fecha,fk_usuarioid)"
                    + "VALUES("
                    + ""+ajuste.getFactor()+","
                    + "'"+ajuste.getCometario()+"',"
                    + ""+ajuste.isTiempo()+","
                    + ""+ajuste.getFk_periodoId()+","
                    + ""+ajuste.getFk_convenioId()+","
                    + ""+ajuste.isInterno()+","
                    + "NOW(),"
                    + ""+ajuste.getFk_usuarioId()+""
                    + ")";
                    int x=DBManager.insert(sql);
                    if(x <= 0){
                        return false;
                    }

        } catch (Exception e) {
            return false;
        }
       return true;
    }

    public boolean deleteAjuste(int ajusteid){
        String sql="";
        try {
            sql="DELETE FROM facturaajuste WHERE ajusteid="+ajusteid;
            DBManager.Update(sql);

        } catch (Exception e) {
            return false;
        }
        return true;

    }


}
